package com.hmdp.service.impl;

import ch.qos.logback.classic.spi.EventArgUtil;
import cn.hutool.core.util.RandomUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hmdp.dto.LoginFormDTO;
import com.hmdp.dto.Result;
import com.hmdp.dto.UserDTO;
import com.hmdp.entity.User;
import com.hmdp.mapper.UserMapper;
import com.hmdp.service.IUserService;
import com.hmdp.utils.RegexUtils;
import com.hmdp.utils.SystemConstants;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.connection.StringRedisConnection;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.Random;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author 虎哥
 * @since 2021-12-22
 */
@Service
@Slf4j
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {


    @Resource
    private StringRedisTemplate stringRedisTemplate;

    @Autowired
    private UserMapper userMapper;
    /**
     * 发送手机验证码
     */
    public Result sendCode(@RequestParam(value = "phone")String phone, HttpSession session) {
        //校验手机号用正则表达式
        if(RegexUtils.isPhoneInvalid(phone)){
            //验证失败
            Result.fail("手机号码填写错误,请重新填写");
        }
        //符合，生成验证码用随机生成器
        //随机生成6位数 是以下依赖的工具
//         <!--hutool-->
//        <dependency>
//            <groupId>cn.hutool</groupId>
//            <artifactId>hutool-all</artifactId>
//            <version>5.7.17</version>
//        </dependency>
        String code = RandomUtil.randomNumbers(6);

        //保存验证码到session
        session.setAttribute("code",code);

        //发送验证码

        log.debug("发送验证码成功，验证码:{}",code);


       //返回成功
        return Result.ok();
    }

    /**
     * 登录功能
     * @param loginForm 登录参数，包含手机号、验证码；或者手机号、密码
     */
    public Result login(LoginFormDTO loginForm, HttpSession session) {

        //获取手机号
        String phone = loginForm.getPhone();


        //效验验证码和验证码
        if (RegexUtils.isPhoneInvalid(phone)) {
            Result.fail("手机号码填写错误,请重新填写");
        }
        //从session里面
        Object code = session.getAttribute("code");
        String codeLogon = loginForm.getCode();

        if (code == null || !code.toString().equals(codeLogon)) {
             return  Result.fail("验证码填写错误,请重新填写");
        }

        //根据电话号查询一个用户query().eq("phone", phone).one();
        User user = query().eq("phone", phone).one();

        //如果用户不存在则创建，存在则登录成功
        if(user == null){
            //用户不存在创建用户
            User user1 = new User();
            user1.setNickName(SystemConstants.USER_NICK_NAME_PREFIX+RandomUtil.randomNumbers(3));
            user1.setPhone(phone);
            //保存到数据库里面
            save(user1);
        }

        //将UserDto取出User对象秉承存到session里面
        UserDTO userDTO = new UserDTO();
        if (user != null) {
            BeanUtils.copyProperties(user,userDTO);
        }
        session.setAttribute("user",userDTO);

        return  Result.ok();

    }
}

